=begin
Copyright (C) 2014 Enrique Rando

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

http://www.gnu.org/licenses/gpl-2.0.html
=end

=begin

THIS CLASS IS MEANT FOR EDUCATIONAL PURPOSES ONLY

IF YOU NEED TO USE THIS SEARCH ENGINE, PLEASE IMPLEMENT IT IN A RUBY CLASS AS INDICATED IN THE DOCUMENTATION USING THE APPROPIATE APIs

=end

require File.expand_path(File.dirname(__FILE__) + '/../Search_engine.rb')
require 'uri'

class Bing_web < Search_engine
	# Object creation
	# Parameters
	#		max_requests	maximum number of requests
	#		proxy			Proxy name or address. Use nil for no proxy.
	#						By default (:ENV), Ruby 2.1 uses HTTP_PROXY environment variable to set the proxy
	#		proxy_port		Proxy port
	#		proxy_user		Proxy username, if required
	#		proxy_password	Proxy password, if required.
	def initialize(max_requests=3, proxy=:ENV, proxy_port=nil, proxy_user=nil, proxy_password=nil)
		# Regexps for data recognition
		regexps = {
			:begin_result =>/<li class="b_algo"/,
			:end_results => /<li class="b_ans"><div class="b_rs">/,
			:document_type => /<\/cite>&nbsp;&#0183;&#32;Archivo\s+(?<data>.*?)<\/div>/,
			:title => /<a href.*?>(?<data>.*?)<\/a>/,
			:url => /<a href="(?<data>[^"]*?)"/,
			:summary => /<p>(?<data>.*?)<\/p>/,
			:cache => /<div\s*.*?\su="(?<data>.*?)"/
		}

		# Mechanisms used for result paging
		paging = {
			:parameter => "first", 
			:parameter_method => :get, 
			:size_parameter => 'SRCHHPGUSR',
			:size_parameter_method => :cookie,
			:size_parameter_value => 'NEWWND=0&ADLT=OFF&NRSLT=50&NRSPH=2&SRCHLANG=', 
			:size => 50
		}

		# Create the Search Engine
		super(
			:http, "www.bing.com", :default, "/search", "q",
			regexps, paging,
			{}, "", {},
			"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0",
			"Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",	
			max_requests
		)

		# Establish connection
		connect_through(proxy, proxy_port, proxy_user, proxy_password)
	end

	# Request all data for a query (Method redefined to change default values)
	# Parameters
	#	query	query to submit
	#	pause	make pause before request (seconds)
	#	max_results	stop if more than this number of results are retrieved
	def request_all(query, pause=2, max_results=1000)
		super(query, pause, max_results)
	end
	
private

	# Creates the cache info
	def get_cache(result)
		data = super(result)
		
		if data.nil?
			return nil
		else
			parts = data.split("|")
			return "http://cc.bingj.com/cache.aspx?q=x&d=#{parts[2]}&w=#{parts[3]}".force_encoding("ascii-8bit")
		end
	end
	
	# Creates the URLs needed to translate a result
	def get_translate(result)
		return nil
	end
	
	def get_url(result)
		return super(result).gsub("&amp;", "&")
	end
	
	def compute_page_start(x)
		return 1 + @page_size * (x - 1)
	end
end
